<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-11-08
 * Time: 14:42:32
 * Info:
 */

namespace app\admin\controller;

use think\facade\View;
use app\common\model\Keywords as KeywordsModel;
use app\common\model\ContentKeywords as ContentKeywordsModel;

class Keywords extends Base
{

    public function index()
    {
        if ($this->request->isAjax()) {
            $limit = (int)input('limit', 15);
            $page  = (int)input('page', 1);
            $key   = input('key');
            $first = ($page - 1) * $limit;
            $where = function ($query) use ($key) {
                if ( ! empty($key['title'])) {
                    $query->whereLike('title', '%'.$key['title'].'%');
                }
                if (isset($key['status']) && $key['status'] != '') {
                    $query->where('status', $key['status']);
                }
            };
            $count = KeywordsModel::where($where)->count();
            $list  = KeywordsModel::where($where)->limit($first, $limit)->order('id desc')->select()->toArray();
            foreach ($list as $key => $v) {
                $list[$key]['count'] = (new ContentKeywordsModel)->contentCount($v['id']);
            }
            $this->success('ok', $list, $count);
        }

        return View::fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $rule  = [
                'title|关键词' => 'require',
            ];
            $this->validate($param, $rule);
            KeywordsModel::create($param);
            $this->success('添加成功');
        }

        return View::fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $rule  = [
                'title|关键词' => 'require',
            ];
            $this->validate($param, $rule);
            KeywordsModel::update($param);
            $this->success('保存成功');
        }
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->success('参数错误');
        }
        $data = KeywordsModel::find($id);
        View::assign('data', $data);

        return View::fetch();
    }

    public function delete()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error('参数错误');
        }
        $data = KeywordsModel::find($id);
        if (empty($data)) {
            $this->error('获取数据失败');
        }
        $data->delete();
        ContentKeywordsModel::where('keywords_id', $id)->delete();
        $this->success('删除成功');
    }

    public function modify()
    {
        if ($this->request->isAjax()) {
            $id = (int)input('get.id');
            if (empty($id)) {
                $this->error("参数错误");
            }
            $field = input('field');
            $val   = input('val');
            KeywordsModel::update([$field => $val], ['id' => $id]);
            $this->success('操作成功');
        }

    }

    /**
     * 文章添加编辑调取关键词数据
     * @return \support\Response|\think\response\Json|null
     */
    public function get_keyword_cate()
    {
        $data = KeywordsModel::where('status', 1)->select();

        return json(['code' => 0, 'msg' => 'ok', 'data' => $data]);
    }

}
